package com.baichuan.zntx.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.baichuan.common.idempotent.annotation.RepeatSubmit;
import com.baichuan.common.log.annotation.Log;
import com.baichuan.common.web.core.BaseController;
import com.baichuan.common.mybatis.core.page.PageQuery;
import com.baichuan.common.core.domain.R;
import com.baichuan.common.core.validate.AddGroup;
import com.baichuan.common.core.validate.EditGroup;
import com.baichuan.common.log.enums.BusinessType;
import com.baichuan.common.excel.utils.ExcelUtil;
import com.baichuan.zntx.domain.vo.RegionVo;
import com.baichuan.zntx.domain.bo.RegionBo;
import com.baichuan.zntx.service.IRegionService;
import com.baichuan.common.mybatis.core.page.TableDataInfo;

/**
 * 工位管理
 *
 * @author MaZhanliang
 * @date 2024-05-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zntx-app/region")
public class RegionController extends BaseController {

    private final IRegionService regionService;

    /**
     * 查询工位管理列表
     */
    @SaCheckPermission("zntx-app:region:list")
    @GetMapping("/list")
    public TableDataInfo<RegionVo> list(RegionBo bo, PageQuery pageQuery) {
        return regionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工位管理列表
     */
    @SaCheckPermission("zntx-app:region:export")
    @Log(title = "工位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RegionBo bo, HttpServletResponse response) {
        List<RegionVo> list = regionService.queryList(bo);
        ExcelUtil.exportExcel(list, "工位管理", RegionVo.class, response);
    }

    /**
     * 获取工位管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zntx-app:region:query")
    @GetMapping("/{id}")
    public R<RegionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(regionService.queryById(id));
    }

    /**
     * 新增工位管理
     */
    @SaCheckPermission("zntx-app:region:add")
    @Log(title = "工位管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RegionBo bo) {
        return toAjax(regionService.insertByBo(bo));
    }

    /**
     * 修改工位管理
     */
    @SaCheckPermission("zntx-app:region:edit")
    @Log(title = "工位管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RegionBo bo) {
        return toAjax(regionService.updateByBo(bo));
    }

    /**
     * 删除工位管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zntx-app:region:remove")
    @Log(title = "工位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(regionService.deleteWithValidByIds(List.of(ids), true));
    }
}
